package com.example.hadoop.mapreduce.ordermaxamount;

import lombok.Data;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: Brian
 * @Date: 2020/04/28/12:34
 * @Description:
 */
@Data
public class OrderItemBean implements WritableComparable<OrderItemBean> {

    private String itemId;
    private String orderId;
    private DoubleWritable amount;

    @Override
    public int compareTo(OrderItemBean o) {
        int compareTo = this.orderId.compareTo(o.getOrderId());
        if (compareTo == 0) {
            compareTo = -(this.amount.compareTo(o.getAmount()));
        }
        return compareTo;
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeUTF(itemId);
        out.writeUTF(orderId);
        out.writeDouble(amount.get());
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        itemId = in.readUTF();
        orderId = in.readUTF();
        amount = new DoubleWritable(in.readDouble());
    }

    public String toString() {
        return "相同 orderId:" + orderId + "中, 最大的Amount是：" + amount.get();
    }
}
